'use strict';

define(['controllers', '../service/menuService'], function(controllers) {
    controllers.common.controller('LeftController', ['$scope', '$rootScope', '$location', 'MenuService', function($scope, $rootScope, $location, menuService) {

        function locationChange() {
            var menuIdMap = menuService.getMenuIdMap();
            var menuUrlMap = menuService.getMenuUrlMap();

            var path = $location.path();
            var originMenuUrl = menuService.getOriginMenuUrl(path);
            var menu = menuUrlMap[originMenuUrl];
            if (menu != null) {
                if (menu.type == 2) {
                    $scope.isCollectionMenu = true;
                    $scope.collectionMap = menuService.getCollectionMap();
                    $scope.collectionMenuMap = menuService.getCollectionMenuMap();
                    $scope.collectionSortedMap = menuService.getCollectionSortMap();

                    $scope.menuCollectionId = menuService.getCollectionId(path);
                    $scope.collectionCount = 0;
                    for (var collectionId in $scope.collectionMap) {
                        if (isEmpty($scope.menuCollectionId)) {
                            $scope.menuCollectionId = collectionId;
                        }
                        $scope.collectionCount++;
                    }

                    $rootScope.leftMenus = [];
                    if (isNotEmpty($scope.menuCollectionId)) {
                        var collectionPrivilegeIds = $scope.collectionMenuMap[$scope.menuCollectionId];
                        if (collectionPrivilegeIds != null && collectionPrivilegeIds.length > 0) {
                            $scope.leftMenus = [];
                            var allLeftMenus = (menu.children != null && menu.children.length > 0) ? menu.children : menuIdMap[menu.parentId].children;
                            for (var i = 0; i < allLeftMenus.length; i++) {
                                var leftMenu = allLeftMenus[i];
                                for (var j = 0; j < collectionPrivilegeIds.length; j++) {
                                    var collectionPrivilegeId = collectionPrivilegeIds[j];
                                    if (leftMenu.id == collectionPrivilegeId) {
                                        $scope.leftMenus.push(allLeftMenus[i]);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                } else {
                    $scope.isCollectionMenu = false;
                    if (menu.children != null && menu.children.length > 0) {
                        $rootScope.leftMenus = menu.children;
                    } else if(menu.parentId != null) {
                        $rootScope.leftMenus = menuIdMap[menu.parentId].children;
                    } else {
                        $rootScope.leftMenus = [menu];
                    }
                }
            } else {
                $scope.isCollectionMenu = false;
                $rootScope.leftMenus = [];
            }
        }

        $scope.$watch('menuCollectionId', function () {
            if ($scope.isCollectionMenu) {
                var arr = $location.path().split('/');
                arr[2] = $scope.menuCollectionId;
                $location.path(arr.join('/'));
            }
        });

        locationChange();
        $scope.$on('$locationChangeSuccess', function(next, current) {
            locationChange();
        });
    }]);
});